﻿using HPIT_LiuzuPoint.Business;
using HPIT_LiuzuPoint.Entity.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using static MPMS.DTOModel.OperateResult;

namespace HPIT_LiuzuPoint.Web.Controllers
{
    public class GiftController : Controller
    {

        private GiftLevelService _giftLevelService = new GiftLevelService();
        public ActionResult Index()
        {
            ViewBag.GiftList = GetGiftLevelList();

            //获取礼品积分下拉框所需的数据源
            return View();
        }

        private List<SelectListItem> GetGiftLevelList()
        {
           var giftLevelService= _giftLevelService.GetList(c => true).Select(c => new SelectListItem()
            {
                Text = c.EG_GiftName,
                Value = c.EG_ID.ToString()
            }).ToList();

            giftLevelService.Insert(0, new SelectListItem{ Text = "全部", Value = "0" });

         
            return giftLevelService;
        }
        public ActionResult GetPagedGiftList(GiftSearchViewModel viewModel)
        {
            var result = _giftLevelService.GetPagedGiftList(viewModel);
            return Json(result.Data);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View("Edit");
        }

        [HttpPost]
        public ActionResult Add(GiftEditViewModel viewModel)
        {
           var result= _giftLevelService.AddGift(viewModel);
            return Json(result);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var result = _giftLevelService.GetGift(id);
            if (result.State==ResultStatus.Success)
            {
                return View(result.Data);
            }
            else
            {
                return HttpNotFound();
            }
        }

        [HttpPost]
        public ActionResult Edit(GiftEditViewModel viewModel)
        {
           var result= _giftLevelService.EditGetGift(viewModel);
            return Json(result);

        }

        public ActionResult Delete(int deleteId)
        {
           var result= _giftLevelService.DeleteGift(deleteId);
            return Json(result);
        }

    }
}